package com.ytkj.digitalConstruction.service.constructionprocess;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ytkj.digitalConstruction.dao.constructionprocess.ConstructionEventMapper;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionBase;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionEvent;
import com.ytkj.digitalConstruction.entity.user.User;
import com.ytkj.digitalConstruction.enums.construction.ConstructionResultEnum;
import com.ytkj.digitalConstruction.exception.CustomException;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.WholeFlowInfo;

/**
 * 应急事件
 * 
 * @author 宁~
 *
 */
@Service
@Transactional
public class ContructionEmergencyEventsService {

	@Autowired
	private ConstructionBaseService constructionBaseService;
	
	@Autowired
	private ConstructionEventMapper constructionEventMapper;
	
	/**
	 * @Description: 应急事件
	 * @Title: setEmergencyEvents 
	 * @param @param constructionEvent 
	 * @return void
	 * @throws
	 */
	public WholeFlowInfo setEmergencyEvents(ConstructionEvent constructionEvent, User user){
		ConstructionBase base = constructionBaseService.getBaseInfo(constructionEvent.getFlowId());
		if(!ConstructionUtilService.checkNowDate(base.getCreateDate())) {
			new CustomException(ConstructionResultEnum.DATE_NOT_TODAY);
		}
		Date createTime = new Date();
		constructionEvent.setUpdateBy(user.getUserId());
		constructionEvent.setUpdateDate(createTime);
		if (constructionEvent.getEventId() == null) {
			constructionEvent.setCreateBy(user.getUserId());
			constructionEvent.setCreateDate(createTime);
			constructionEventMapper.insertSelective(constructionEvent);
			ConstructionUtilService.isAddSuccess(constructionEvent.getEventId());
		}else {
			ConstructionUtilService.isUpdateSuccess(constructionEventMapper.updateByPrimaryKeySelective(constructionEvent));
		}
		return constructionBaseService.showWholeFlowByFlowId(constructionEvent.getFlowId());
	}
	
}
